package cn.bigcontainer.alt.meta.dto;

import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.meta.po.MProperty;

public class MySqlColumn {
	private String field = null;
	private String type = null;
	private String comment = null;
	private String allowsNull = null;
	private String key = null;
	private String defaultValue = null;
	private String Extra = null;

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getAllowsNull() {
		return allowsNull;
	}

	public void setAllowsNull(String allowsNull) {
		this.allowsNull = allowsNull;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getExtra() {
		return Extra;
	}

	public void setExtra(String extra) {
		Extra = extra;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	/**
	 * mysql类型、MProperty.type以及Java类型的映射数据
	 */
	private final static String[][] TYPE_MAPPER = { { "varchar", MProperty.TYPE_VARCHAR, "String" }, { "text", MProperty.TYPE_TEXT, "String" },
			{ "smallint", MProperty.TYPE_SMALLINT, "Integer" }, { "int", MProperty.TYPE_INTEGER, "Integer" },
			{ "decimal", MProperty.TYPE_NUMERIC, "Double" }, { "date", MProperty.TYPE_DATE, "String" },
			{ "datetime", MProperty.TYPE_DATETIME, "String" } };

	/**
	 * @param logicType
	 * @return
	 */
	public static String getTypeByLogicType (String logicType) {
		for (String[] m : TYPE_MAPPER) {
			if (m[1].equals(logicType)) {
				return m[0];
			}
		}
		return TYPE_MAPPER[0][0];
	}
	
	public String getXType(int index) {
		String type = this.type.toLowerCase();
		int idx = type.indexOf('(');
		if (idx > 0) {
			type = type.substring(0, idx);
		}
		for (String[] m : this.TYPE_MAPPER) {
			if (m[0].equals(type)) {
				return m[index];
			}
		}
		return this.TYPE_MAPPER[0][index];
	}

	public String getLogicType() {
		return this.getXType(1);
	}

	public String getJavaType() {
		return this.getXType(2);
	}

	/**
	 * 根据逻辑模型实体类型获取java类型
	 * @param logicType 逻辑模型实体类型
	 * @return
	 */
	public static String getJavaType (String logicType) {
		for (String[] m : TYPE_MAPPER) {
			if (m[1].equals(logicType)) {
				return m[2];
			}
		}
		return TYPE_MAPPER[0][2];
	}
	
	public String getTypeDetail() {
		int idx = this.type.indexOf('(');
		if (idx < 1) {
			return null;
		}
		return this.type.substring(idx + 1, type.length() - 1);
	}

	public boolean isPrimaryKey() {
		return key != null && key.equals("PRI");
	}

	public boolean isNull() {
		return allowsNull != null && allowsNull.equals("YES");
	}

	public static String getDefaultJavaValue (String javaType, Object defaultValue) {
		if (defaultValue == null) {
			return "null";
		}
		if (javaType.equals("Double") || javaType.equals("Integer")) {
			if (defaultValue.equals("")) {
				return "null";
			}
			return defaultValue.toString();
		}
		return "\"" + defaultValue + "\"";
	}
	
	public String getDefaultJavaValue() {
		return getDefaultJavaValue (this.getJavaType(), this.getDefaultValue());
	}

	public String getDescription() {
		if (WoKit.isEmpty(comment)) {
			return this.field;
		}
		return this.comment;
	}
}
